package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsNotifyInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsNotifyInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.NotifyInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.NoticeInfoBean;
import cc.rengu.igas.bsps.facade.request.NoticeInfoQueryRequest;
import cc.rengu.igas.bsps.facade.response.NoticeInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName NoticeInfoQueryService
 * @Description 公告信息查询
 * @Author 赵奎伍
 * @date 2020/4/5
 */
public class NoticeInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        NoticeInfoQueryRequest noTransMchntListQueryRequest = new NoticeInfoQueryRequest();
        ConvertUtil.convertOutput(noTransMchntListQueryRequest);
        return noTransMchntListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        NoticeInfoQueryRequest requestParams = (NoticeInfoQueryRequest) request;
        NoticeInfoQueryResponse responseParams = new NoticeInfoQueryResponse();

        String userId = requestParams.getUserId();
        String instId = requestParams.getHeader().getInstId();
        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(userId)) {
            rglog.error("用户编号<{}>不能为空!", userId);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 通过机构号查询对应的数据 */
        BspsNotifyInfoMapper bspsNotifyInfoMapper = new BspsNotifyInfoMapperImpl();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(requestParams.getPageIndex());
        pageInfo.setPageSize(requestParams.getPageSize());
        PageResult pageResult = bspsNotifyInfoMapper.selectBspNotifyInfoByInstId(instId, pageInfo);
        BizResponse<NoticeInfoQueryResponse> bizResponse = new BizResponse<>();
        if (null == pageResult) {

            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setResult(responseParams);
            return bizResponse;
        }
        responseParams.setTotalPage(pageResult.getPageInfo().getTotalPage());
        responseParams.setTotalRows(pageResult.getPageInfo().getTotalRows());
        List<NoticeInfoBean> noticeInfoBeanList = new ArrayList<>();
        for (Object object : pageResult.getResult()) {
            NoticeInfoBean noticeInfoBean = new NoticeInfoBean();
            NotifyInfo notifyInfo = (NotifyInfo) object;
            noticeInfoBean.setNoticeId(notifyInfo.getId());
            noticeInfoBean.setMessage(notifyInfo.getMessage());
            noticeInfoBean.setTitle(notifyInfo.getTitle());
            noticeInfoBean.setPublishTime(notifyInfo.getPublishTime());
            noticeInfoBeanList.add(noticeInfoBean);
        }
        responseParams.setNoticeInfoList(noticeInfoBeanList);
        responseParams.setPageIndex(responseParams.getPageIndex());
        responseParams.setPageSize(responseParams.getPageSize());
        /* 组装应答报文 */
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(responseParams);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        NoticeInfoQueryResponse noTransMchntListQueryResponse = (NoticeInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(noTransMchntListQueryResponse);
    }
}
